﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace TMSimulator.Forms
{
    public partial class DefaultForm : Form
    {
        public DefaultForm()
        {
            InitializeComponent();
            Controller.InitializeSystem(@"..\..\data\SerializedDB.bin", @"..\..\data\logfile.txt");
            UserManager.UserIndex = (from entry in UserManager.UserIndex orderby entry.Key descending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);
            foreach (string name in UserManager.UserIndex.Keys) // show the users in the list box in the form
            {
                checkedListBox1.Items.Add(name);
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
           
          string name= checkedListBox1.Items[checkedListBox1.SelectedIndex].ToString();
          chart1.Series.Add(name);
            chart1.Series[name].ChartType = SeriesChartType.Line;  // graph type
            
            foreach (KeyValuePair<FileAccess, double> pair in UserManager.UserIndex[name].History)
            {
              
                //pair.Key.Time.GetDateTimeFormats()
                string time = pair.Key.Time.Day.ToString() + '/' + pair.Key.Time.Month.ToString() + '/' + pair.Key.Time.Year;
                chart1.Series[name].Points.AddXY(time, Math.Round(pair.Value, 3, MidpointRounding.AwayFromZero));

            }

   
                
            }
  
        private void chart1_GetToolTipText(object sender, ToolTipEventArgs e)
        {
              // Check selevted chart element and set tooltip text
            if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)
            {
                        int i = e.HitTestResult.PointIndex;
                DataPoint dp = e.HitTestResult.Series.Points[i];
                e.Text = string.Format("{0:F1}", dp.YValues[0]);
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void dBInitializationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DBInitialization init = new DBInitialization();
            init.ShowDialog();
        }

        private void simulatorSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Simulator sim = new Simulator();
            sim.ShowDialog();
        }

        private void checkedListBox1_ControlRemoved(object sender, ControlEventArgs e)
        {

        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
          


        }
     

      
       





    }
}
